package com.hhf.springboot.utils;

import lombok.SneakyThrows;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @Author hhf
 * @Date 2025/1/11
 * @Description 日期工具类
 */
public class DateUtils {

    private static final String INIT_DATE = "1970-01-01 08:00:00";
    private static final DateFormat DEFAULT_DF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static final String FMT_YMD = "yyyy-MM-dd";

    public static Date getNow() {
        return new Date();
    }

    public static String format(Date date) {
        return DEFAULT_DF.format(date);
    }

    public static String format(Date date, String fmt) {
        return new SimpleDateFormat(fmt).format(date);
    }

    public static Date dayAfter(Date date, int days) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, days);
        return cal.getTime();
    }

    public static Date getZeroDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
    }

    @SneakyThrows
    public static Date parse(String dateStr, String fmt) {
        return new SimpleDateFormat(fmt).parse(dateStr);
    }


    @SneakyThrows
    public static Date parse(String dateStr) {
        return DEFAULT_DF.parse(dateStr);
    }

    @SneakyThrows
    public static Date getInitDate() {
        return DEFAULT_DF.parse(INIT_DATE);
    }

    /**
     * 计算两个日期相差天数
     *
     * @param beginDay 开始日期
     * @param endDay   结束日期
     * @return 相差天数
     */
    public static int diffDay(Date beginDay, Date endDay) {
        long time = Math.abs(endDay.getTime() - beginDay.getTime());
        return (int) (time / 1000 / 60 / 60 / 24);
    }

    /**
     * 获取一天的开始时间
     * @param date 日期
     * @return 一天的开始时间
     */
    public static Date getDayStartTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 获取一天的结束时间
     * @param date 日期
     * @return 一天的结束时间
     */
    public static Date getDayEndTime(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }
    
    /**
     * 获取本周的开始时间
     * @return 本周的开始时间
     */
    public static Date getBeginDayOfWeek() {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek());
        return getDayStartTime(calendar.getTime());
    }
    
    /**
     * 获取本周的结束时间
     * @return 本周的结束时间
     */
    public static Date getEndDayOfWeek() {
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek() + 6);
        return getDayEndTime(calendar.getTime());
    }

    /**
     * 获取当月第一天的开始时间
     *
     * @return 当月第一天的开始时间
     */
    public static Date getBeginDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return getDayStartTime(calendar.getTime());
    }

    /**
     * 获取当月最后一天的结束时间
     *
     * @return 当月最后一天的结束时间
     */
    public static Date getEndDayOfMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return getDayEndTime(calendar.getTime());
    }

    /**
     * 获取当年第一天的开始时间
     *
     * @return 当年第一天的开始时间
     */
    public static Date getBeginDayOfYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        return getDayStartTime(calendar.getTime());
    }

    /**
     * 获取当年最后一天的结束时间
     *
     * @return 当年最后一天的结束时间
     */
    public static Date getEndDayOfYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.getActualMaximum(Calendar.DAY_OF_YEAR));
        return getDayEndTime(calendar.getTime());
    }
}
